package networkcontroller.xml;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * class describing search packet data
 */
public class XMLSearch extends XML
{
	/**
	 * creates xml search
	 * 
	 * @param host
	 *            source host
	 * @param port
	 *            source host port
	 * @param id
	 *            id of search
	 * @param name
	 *            name criteria
	 * @param sizeMin
	 *            minimal size criteria
	 * @param sizeMax
	 *            maximal size criteria
	 * @param sum
	 *            hash of file criteria
	 */
	public XMLSearch(String host, int port, int id, String name, int sizeMin, int sizeMax, String sum)
	{
		/*
		 * <search> <sender> <host></host> <port></port> </sender> <id></id>
		 * <name></name> <sizemin></sizemin> <sizemax></sizemax> <sum></sum>
		 * </search>
		 */
		super();
		createNewDocument();
		Node searchNode = document.createElement("search");
		document.appendChild(searchNode);
		Node senderNode = document.createElement("sender");
		searchNode.appendChild(senderNode);
		Node hostNode = document.createElement("host");
		hostNode.setTextContent(host);
		senderNode.appendChild(hostNode);
		Node portNode = document.createElement("port");
		portNode.setTextContent(String.valueOf(port));
		senderNode.appendChild(portNode);
		Node idNode = document.createElement("id");
		idNode.setTextContent(String.valueOf(id));
		searchNode.appendChild(idNode);
		if (name != null)
		{
			Node nameNode = document.createElement("name");
			nameNode.setTextContent(name);
			searchNode.appendChild(nameNode);
		}
		if (sizeMin != 0)
		{
			Node sizeMinNode = document.createElement("sizemin");
			sizeMinNode.setTextContent(String.valueOf(sizeMin));
			searchNode.appendChild(sizeMinNode);
		}
		if (sizeMax != 0)
		{
			Node sizeMaxNode = document.createElement("sizemax");
			sizeMaxNode.setTextContent(String.valueOf(sizeMax));
			searchNode.appendChild(sizeMaxNode);
		}
		if (sum != null)
		{
			Node sumNode = document.createElement("sum");
			sumNode.setTextContent(sum);
			searchNode.appendChild(sumNode);
		}
	}

	/**
	 * creates xml search from data
	 * 
	 * @param XMLData
	 *            byte array of data
	 */
	public XMLSearch(byte[] XMLData)
	{
		super(XMLData);
	}

	/**
	 * gets source host
	 * 
	 * @return source host
	 */
	public String getHost()
	{
		NodeList hosts = document.getElementsByTagName("host");
		return hosts.item(0).getTextContent();
	}

	/**
	 * gets source host port
	 * 
	 * @return source host port
	 */
	public int getPort()
	{
		NodeList ports = document.getElementsByTagName("port");
		return Integer.parseInt(ports.item(0).getTextContent());
	}

	/**
	 * gets id of search associated with results
	 * 
	 * @return id of search
	 */
	public int getId()
	{
		NodeList ids = document.getElementsByTagName("id");
		return Integer.parseInt(ids.item(0).getTextContent());
	}

	/**
	 * gets name criteria
	 * 
	 * @return name criteria, null if absent
	 */
	public String getName()
	{
		NodeList names = document.getElementsByTagName("name");
		if (names.item(0) == null)
			return null;
		return names.item(0).getTextContent();
	}

	/**
	 * gets minimal size criteria
	 * 
	 * @return minimal size, 0 if absent
	 */
	public int getSizeMin()
	{
		NodeList sizeMins = document.getElementsByTagName("sizemin");
		if (sizeMins.item(0) == null)
			return 0;
		return Integer.parseInt(sizeMins.item(0).getTextContent());
	}

	/**
	 * gets maximal size criteria
	 * 
	 * @return maximal size, 0 if absent
	 */
	public int getSizeMax()
	{
		NodeList sizeMaxs = document.getElementsByTagName("sizemax");
		if (sizeMaxs.item(0) == null)
			return 0;
		return Integer.parseInt(sizeMaxs.item(0).getTextContent());
	}

	/**
	 * gets hash of file criteria
	 * 
	 * @return hash of file, null if absent
	 */
	public String getSum()
	{
		NodeList sums = document.getElementsByTagName("sum");
		if (sums.item(0) == null)
			return null;
		return sums.item(0).getTextContent();
	}
}
